package com.wyl.libbase.base

import android.view.View
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableInt
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.wyl.libbase.extend.logD


abstract class PageViewModel : BaseViewModel() {
    protected var page = 1
    protected val pageSize = 10
    protected var totalSize = page * pageSize

    val loadListener by lazy {
        object : RefreshListenerAdapter() {
            override fun onRefresh(refreshLayout: TwinklingRefreshLayout) {
                refresh.set(true)
                refresh()
            }

            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout) {
                loadMore.set(true)
                loadMore()
            }

            override fun onRefreshCanceled() {
                logD("onRefreshCanceled")
            }

            override fun onFinishRefresh() {
                logD("onFinishRefresh")
            }

        }

    }

    open val enableRefresh = ObservableBoolean(true)

    open val enableLoadMore = ObservableBoolean(true)

    /**
     * 是否正在刷新
     */
    val refresh = ObservableBoolean(false)

    /**
     * 是否正在加载更多
     */
    val loadMore = ObservableBoolean(false)

    /**
     * 是否展示空页面
     */
    val empty = ObservableInt(View.GONE)

    abstract fun refresh()

    abstract fun loadMore()
}